import { refreshToken } from "../../api/index";
import { useUserStoreWithOut } from '@/store'
let isLoading = false;

export function refreshTokenTime(expiresIn){
    if(isLoading) return;
    
    let expireSeconds = expiresIn - Date.now() / 1000
    if(expireSeconds <= 5 * 60 && expireSeconds > 0){
        // token即将过期，刷新token
        isLoading = true
        refreshToken().then(res => {
            isLoading = false
            const userStorage = useUserStoreWithOut()
            userStorage.updateToken(res.data)
        }).catch(err => {
            isLoading = false
            console.log(err)
        })
    }
}
